package com.ryenlii.datatrans.core.tcp.service;

import com.ryenlii.datatrans.core.kafka.KafkaService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;

/**
 * 通道初始化
 */
public class BootNettyChannelInitializer<SocketChannel> extends ChannelInitializer<Channel> {

    private final KafkaService kafkaService;

    public BootNettyChannelInitializer(KafkaService kafkaService) {
        this.kafkaService = kafkaService;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {

        // ChannelOutboundHandler，依照逆序执行
        //        ch.pipeline().addLast("encoder", new StringEncoder(Charset.forName("utf8")));
        ch.pipeline().addLast("encoder", new MyEncoder());

        // 属于ChannelInboundHandler，依照顺序执行
        //        ch.pipeline().addLast("decoder", new StringDecoder(StandardCharsets.UTF_8));
        ch.pipeline().addLast("decoder", new MyDecoder());
        /**
         * 自定义ChannelInboundHandlerAdapter
         */
        ch.pipeline().addLast(new BootNettyChannelInboundHandlerAdapter(kafkaService));
    }

}
